home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-06-16 | 7.3 KB | 289 lines | [TEXT/CWIE] |
- /*
- File: StNavServices.cp
-
- Copyright: © 1998 by Apple Computer, Inc., all rights reserved.
-
- */
-
- // Stack Based classes for Navigation Services calls - NavGetFile, NavPutFile
-
- //
- // You may incorporate this sample code into your applications
- // without restriction. This sample code has been provided "AS
- // IS" and the responsibility for its operation is 100% yours.
- // You are not permitted to redistribute the source as "Apple
- // sample code" after having made changes. If you're going to
- // re-distribute the source, we require that you make it clear
- // in the source that the code was descended from Apple sample
- // code, but that you've made changes.
- //
-
- #include <StNavServices.h>
-
- #ifndef _H_UAppleEventsMgr
- #include <UAppleEventsMgr.h>
- #endif
-
- #ifndef _H_UMemoryMgr
- #include <UMemoryMgr.h>
- #endif
-
-
- StNavGetFile::StNavGetFile ( OSErr *result,
- void *specs,
- long *numspecs,
- StNavGetFileOpenEventUPP openProc,
- NavReplyRecord *reply,
- Boolean getOneFile,
- Boolean showAllReadableFiles,
- NavEventUPP eventProc,
- NavPreviewUPP previewProc,
- NavObjectFilterUPP filterProc,
- NavTypeListHandle typeList,
- NavCallBackUserData eventProcUD ) :
- fTheirSpecs(nil),
- fTheirEventUPP(eventProc),
- fTheirEventUD(eventProcUD)
- {
- if ( (*result = Init ( )) != noErr )
- return;
- // do some parameter checking
- *result = paramErr;
- if ( specs == nil && reply == nil && openProc == nil )
- return; // gotta have one of these
-
- fReply.data.Adopt ( new NavReplyRecord );
-
- // use default options
- NavGetDefaultDialogOptions ( &fdialogOptions );
-
- // only a single file opened
- if ( getOneFile )
- fdialogOptions.dialogOptionFlags -= kNavAllowMultipleFiles; // only a single file
-
- // show all readables
- if ( showAllReadableFiles )
- fdialogOptions.dialogOptionFlags += kNavSelectAllReadableItem;
-
- // try to use file-based open resource for translation information if none given
- NavTypeListHandle types = typeList;
- if ( types == nil )
- types = (NavTypeListHandle)::Get1Resource ( 'open', 128 ); // try caller's open info
-
- // go do it
- *result = NavGetFile ( nil, fReply.data.Get(), &fdialogOptions,
- fTheirEventUPP, previewProc, filterProc,
- types, fTheirEventUD );
-
- if ( *result != noErr )
- return;
-
- long theCount;
- *result = AECountItems ( &fReply.data->selection, &theCount );
- if ( *result != noErr )
- return;
-
- FSSpecArrayPtr theirSpecs = (FSSpecArrayPtr) specs;
-
- // see if caller wanted an array of FSSpecs returned
- if ( specs != nil && !getOneFile )
- {
- theirSpecs = (FSSpecArrayPtr) NewPtrClear ( sizeof ( FSSpec ) * theCount );
- if ( (*result = MemError()) != noErr )
- return;
- *(void**)specs = theirSpecs;
- fTheirSpecs = theirSpecs;
- }
-
- // return count information
- if ( numspecs != nil )
- *numspecs = theCount;
-
- // fill in reply structs, and make any open callbacks
- for ( int i = 0; i < theCount; i++ )
- {
- StAEDescriptor desc;
- *result = AEGetNthDesc( &fReply.data->selection, i+1, typeWildCard, nil, desc );
- if ( *result != noErr )
- return;
- FSSpec theSpec;
- try
- {
- StNavGetFile::GetFSSpecFromAEDesc ( desc, theSpec ); // put reply in FSSpec
- }
- catch ( OSErr err )
- {
- *result = err;
- return;
- }
-
- if ( specs != nil )
- *theirSpecs++ = theSpec; // copy spec to user's array
-
- if ( openProc != nil )
- CallUniversalProc ( openProc, StNavGetFileOpenProcInfo, &theSpec );
- }
-
- // SUCCESS
- // if user supplies the reply, copy reply data
- if ( reply != nil )
- {
- *reply = *fReply.data.Get(); // copy reply to user's reply
- delete fReply.data.Release(); // user must call NavDisposeReply
- }
- }
-
- StNavGetFile::~StNavGetFile ( )
- {
- // if caller wanted an FSSpecArray, clean up
- if ( fTheirSpecs != nil )
- DisposePtr ( (Ptr)fTheirSpecs );
- }
-
-
- StNavPutFile::StNavPutFile ( OSErr *result,
- OSType fileType,
- OSType fileCreator,
- FSSpecPtr spec,
- NavReplyRecord *reply,
- NavEventUPP eventProc,
- NavCallBackUserData eventProcUD ) :
- fresult(result),
- fTheirEventUPP(eventProc),
- fTheirEventUD(eventProcUD)
- {
-
- if ( (*result = StNavGetFile::Init ( )) != noErr )
- return;
- // do some parameter checking
- *result = paramErr;
- if ( spec == nil && reply == nil )
- return; // gotta have one of these
-
- fReply.data.Adopt ( new NavReplyRecord );
-
- NavGetDefaultDialogOptions ( &fdialogOptions );
-
- *result = NavPutFile ( nil, fReply.data.Get(), &fdialogOptions,
- fTheirEventUPP, fileType, fileCreator,
- fTheirEventUD );
- if ( *result != noErr )
- return;
-
- if ( spec != nil )
- {
- StAEDescriptor desc;
- *result = AEGetNthDesc( &fReply.data->selection, 1, typeWildCard, nil, desc );
- if ( *result != noErr )
- return;
- FSSpec theSpec;
- try
- {
- StNavGetFile::GetFSSpecFromAEDesc ( desc, theSpec ); // put reply in FSSpec
- }
- catch ( OSErr err )
- {
- *result = err;
- return;
- }
- *spec = theSpec;
- }
-
- if ( reply != nil )
- *reply = *fReply.data.Get(); // copy reply to user's reply
-
- }
-
- StNavPutFile::~StNavPutFile ( )
- {
- if ( *fresult == noErr )
- *fresult = NavCompleteSave ( fReply.data.Get(), kNavTranslateInPlace );
- }
-
- OSType StNavGetFile::GetApplSignature ( )
- {
- ProcessSerialNumber PSN;
- ProcessInfoRec info;
- Str31 processName;
- FSSpec FileSpec;
- info.processInfoLength = sizeof ( ProcessInfoRec );
- info.processName = processName;
- info.processAppSpec = &FileSpec;
- OSErr err = GetCurrentProcess(&PSN);
- GetProcessInformation(&PSN, &info);
- return info.processSignature;
- }
-
- NavTypeListHandle StNavGetFile::MakeTypeList ( OSType applSignature, int numTypes, OSType *types )
- {
- NavTypeListHandle h = (NavTypeListHandle) NewHandleClear ( sizeof (NavTypeList) +
- (sizeof (OSType) * (numTypes-1)) );
- if ( h != nil )
- {
- (*h)->componentSignature = (applSignature == 0L) ?
- StNavGetFile::GetApplSignature ( ) : applSignature;
- (*h)->reserved = 0;
- (*h)->osTypeCount = numTypes;
- for ( int i = 0; i < numTypes; i++ )
- {
- (*h)->osType[i] = types[i];
- }
- }
- return h;
-
- }
-
- NavTypeListHandle StNavGetFile::MakeTypeList ( OSType applSignature, int numTypes, ... )
- {
- va_list argptrs;
- va_start ( argptrs, numTypes );
- NavTypeListHandle h = (NavTypeListHandle) NewHandleClear ( sizeof (NavTypeList) +
- (sizeof (OSType) * (numTypes-1)) );
- if ( h != nil )
- {
- (*h)->componentSignature = (applSignature == 0L) ?
- StNavGetFile::GetApplSignature ( ) : applSignature;
- (*h)->reserved = 0;
- (*h)->osTypeCount = numTypes;
- for ( int i = 0; i < numTypes; i++ )
- {
- OSType theType = va_arg ( argptrs, OSType );
- (*h)->osType[i] = theType;
- }
- }
- va_end ( argptrs );
- return h;
- }
-
- void StNavGetFile::GetFSSpecFromAEDesc ( AEDesc &inDesc, FSSpec &outValue )
- {
- Handle dataH;
- AEDesc coerceDesc = {typeNull, nil};
-
- if (inDesc.descriptorType == typeFSS) {
- dataH = inDesc.dataHandle; // Descriptor is the type we want
-
- } else { // Try to coerce to the desired type
- if (::AECoerceDesc(&inDesc, typeFSS, &coerceDesc) == noErr) {
- // Coercion succeeded
- dataH = coerceDesc.dataHandle;
-
- } else { // Coercion failed
- throw (errAETypeError);
- }
- }
-
- outValue = **(FSSpec**) dataH; // Extract value from Handle
- if (coerceDesc.dataHandle != nil) {
- ::AEDisposeDesc(&coerceDesc);
- }
- }
-
- OSErr StNavGetFile::Init ( )
- {
- if ( !NavServicesCanRun ( ) )
- return kNavInvalidSystemConfigErr;
-
- return noErr;
- }
-